IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'RealmsGet')
	BEGIN
		DROP  Procedure  RealmsGet
	END

GO

CREATE Procedure RealmsGet
@SagaID int,
@CreatorID int = Null
AS
IF(@CreatorID Is Null)
Begin
Select
r.RealmID,
r.CreatorID,
r.IsPrivate,
r.SagaID,
r.Title,
r.Summary,
r.ParentID
From Realm r
Where r.SagaID = @SagaID
End
Else
Begin
Select
r.RealmID,
r.CreatorID,
r.IsPrivate,
r.SagaID,
r.Title,
r.Summary,
r.ParentID
From Realm r
Where r.SagaID = @SagaID And r.ISPrivate = 0 And  r.CreatorID <> @CreatorID 
Union ALL
Select
r.RealmID,
r.SagaID,
r.CreatorID,
r.IsPrivate,
r.Title,
r.Summary,
r.ParentID
From Realm r
Where r.SagaID = @SagaID and r.CreatorID = @CreatorID 

End

GO



